/*
 * Copyright 2004-2011 Paolo Conte
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jellylab.ojoverse.types;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Blob;
import java.sql.Types;

public abstract class BaseTypes {

    public TypeStruct varchar() {
        return varchar(0);
    }

    public TypeStruct varchar(int lenght) {
        return new TypeStruct(Types.VARCHAR, lenght, 0);
    }

    public TypeStruct aChar() {
        return aChar(0);
    }

    public TypeStruct aChar(int lenght) {
        return new TypeStruct(Types.CHAR, lenght, 0);
    }

    public TypeStruct integer() {
        return new TypeStruct(Types.INTEGER, 0, 0);
    }

    public TypeStruct bigint() {
        return new TypeStruct(Types.BIGINT, 0, 0);
    }

    public TypeStruct numeric(int precision) {
        return numeric(precision, 0);
    }

    public TypeStruct numeric(int precision, int scale) {
        return new TypeStruct(Types.NUMERIC, precision, scale);
    }

    public TypeStruct aDouble() {
        return new TypeStruct(Types.DOUBLE, 0, 0);
    }

    public class TypeStruct {

        private int type;
        private int lenght = 0;
        private int scale = 0;

        public TypeStruct(int type, int lenght, int scale) {
            this.type = type;
            this.lenght = lenght;
            this.scale = scale;
        }

        public int getType() {
            return this.type;
        }

        public int getLenght() {
            return this.lenght;
        }

        public int getPrecision() {
            return this.lenght;
        }

        public int getScale() {
            return this.scale;
        }
    }

    public static boolean isType(int colType, int type)
	{
		return (colType == type);
	}

    public static Class getTypeClass(int type) {
        switch (type) {
            case Types.BIT:
                return Boolean.class;
            case Types.TINYINT:
                return Byte.class;
            case Types.INTEGER:
                return Integer.class;
            case Types.BIGINT:
                return Long.class;
            case Types.FLOAT:
            case Types.REAL:
                return Float.class;
            case Types.DOUBLE:
                return Double.class;
            case Types.DECIMAL:
                return BigDecimal.class;
            case Types.DATE:
                return Date.class;
            case Types.TIME:
                return Time.class;
            case Types.TIMESTAMP:
                return Timestamp.class;
            case Types.CHAR:
            case Types.VARCHAR:
            case Types.LONGVARCHAR:
                return String.class;
            case Types.BINARY:
                return String.class;	// TODO: verify other options: java.sql.Blob or byte[]
            case Types.BLOB:
                return Blob.class;
            default:
                return null;
        }
    }

    /*
    public static final int NULL = 0;
    public static final int BIT = 1;    // java.sql.Types.BIT
    public static final int BYTE = 2;
    public static final int INT = 4;
    public static final int SHORTINT = 8;
    public static final int BIGINT = 16;
    public static final int PICOINT = 32;
    public static final int FLOAT = 64;
    public static final int REAL = 126;		//FLOAT
    public static final int DOUBLE = 256;
    public static final int DECIMAL = 512;
    public static final int DATE = 1024;
    public static final int TIME = 2048;
    public static final int DATETIME = 4096;
    public static final int TIMESTAMP = 8192;
    public static final int CHAR = 16384;
    public static final int VARCHAR = 32768;
    public static final int TEXT = 65536;
    public static final int BINARY = 131072;
    public static final int BLOB = 262144;
    public static final int OBJECT = 524288;
    public static final int CUSTOM = 1048576;

    public static boolean isDatatype(int colDatatype, int datatype) {
    return (colDatatype & datatype) > 0;
    }
    public static Class mapDatatype(int datatype) {
    switch (datatype) {
    case BIT:
    return Boolean.class;
    case BYTE:
    case SHORTINT:
    return Byte.class;
    case INT:
    return Integer.class;
    case BIGINT:
    return Long.class;
    case FLOAT:
    case REAL:
    return Float.class;
    case DOUBLE:
    return Double.class;
    case DECIMAL:
    return BigDecimal.class;
    case DATE:
    case DATETIME:
    return Date.class;
    case TIME:
    return Time.class;
    case TIMESTAMP:
    return Timestamp.class;
    case CHAR:
    case VARCHAR:
    case TEXT:
    return String.class;
    case BINARY:
    return String.class;	// TODO: verify other options: java.sql.Blob or byte[]
    case BLOB:
    return Blob.class;
    default:
    return null;
    }
    }
     */
    /*
    // only for JDK >= 1.7
    public static Class mapDatatype(String datatype) {
    switch (datatype.toUpperCase()) {
    case "BIT":
    return Boolean.class;
    case "BYTE":
    case "SHORTINT":
    return Byte.class;
    case "INTEGER":
    return Integer.class;
    case "BIGINT":
    return Long.class;
    case "FLOAT":
    case "REAL":
    return Float.class;
    case "DOUBLE":
    return Double.class;
    case "DECIMAL":
    return BigDecimal.class;
    case "DATE":
    case "DATETIME":
    return Date.class;
    case "TIME":
    return Time.class;
    case "TIMESTAMP":
    return Timestamp.class;
    case "CHAR":
    case "VARCHAR":
    return String.class;
    case "TEXT":
    return String.class;
    case "BINARY":
    return String.class;	// TODO: verify other options: java.sql.Blob or byte[]
    case "BLOB":
    return Blob.class;
    default:
    return null;
    }
    }
     */
}
